<?php
/**
 * @version $Id: controller.php 18 2009-12-07 11:22:56Z truongnhan0311 $
 * @author truongnhan0311
 * @package	wrd component
 */
	// no direct access
	defined( '_JEXEC' ) or die( 'Restricted access' );
	$list_products = $this->list_products;
	$list_image = $this->list_image ;
?>
<script>
	var user_points = '<?php echo $this->p_o_u ; ?>';
	
	jQuery(document).ready(function() {
		jQuery("#ajax-loading-ck").find("input").keydown(function(event) {
	        // Allow: backspace, delete, tab, escape, and enter
	        if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
	             // Allow: Ctrl+A
	            (event.keyCode == 65 && event.ctrlKey === true) || 
	             // Allow: home, end, left, right
	            (event.keyCode >= 35 && event.keyCode <= 39)) {
	                 // let it happen, don't do anything
	                 return;
	        }
	        else {
	            // Ensure that it is a number and stop the keypress
	            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
	                event.preventDefault(); 
	            }   
	        }
	    });
	    
		jQuery(".point").click(function() {
			var qty = jQuery(this).attr('qty');
			var points = parseInt(jQuery(this).attr('rel1'));
			if (jQuery(qty).val() != '0') {
				jQuery(qty).attr('rel2', 'p');
				var total_points = parseInt(jQuery(qty).val()) * points;
	
				if (parseInt(user_points) - total_points < 0) {
					jQuery(qty).val("0");
				} else {
					var dest = jQuery(this).attr('destination');
					jQuery(dest).html("<br/><br/><font size='5' color='blue'>" + total_points + " pts </font> ");
				}
			}
		});
		
		jQuery(".money").click(function() {
			var qty = jQuery(this).attr('qty');
			var points = parseInt(jQuery(this).attr('rel1'));
			var money = parseInt(jQuery(this).attr('rel2'));
			if (jQuery(qty).val() != '0') {
				jQuery(qty).attr('rel2', 'pm');
				var total_points = parseInt(jQuery(qty).val()) * points;
				var total_money = parseInt(jQuery(qty).val()) * money;
				
				if (parseInt(user_points) - total_points < 0) {
					jQuery(qty).val("0");
				} else {
					var dest = jQuery(this).attr('destination');
					jQuery(dest).html("<br/><font size='5' color='blue'>" + total_points + " pts </font> <hr /> <font size='5'color='red'>" + total_money + " vnd</font>");
				}
			}
		});

		// clear cart
		jQuery("#clearCart").click(function() {
			jQuery("#ajax-loading-ck").find("input").each(function() {
				var product_id = jQuery(this).attr('rel');
				var qty = jQuery(this).val("0");
			});

			jQuery(".displayPointAndCash").html("");
		});

		// submit cart
		jQuery("#submitCart").click(function() {
			var products = "";
			jQuery("#ajax-loading-ck").find("input").each(function() {
				var method = jQuery(this).attr('rel2');
				var qty = jQuery(this).val();
				var product_id = jQuery(this).attr('rel1');
				
				if (method != '' && qty != '0') {
					products += "&product[]=" + method + ";" + product_id + ";" + qty;
				}
			});

			if (products != "") {
				var url = "<?php echo JRoute::_("index.php?option=com_wrd&view=redemption&layout=process")?>"  + products;
				jQuery.get(url, function(data) {
					  jQuery.facebox(data);

					  jQuery("#ajax-loading-ck").find("input").each(function() {
							var product_id = jQuery(this).attr('rel');
							var qty = jQuery(this).val("0");
						});

					  jQuery(".displayPointAndCash").html("");
				});
			} else {
				alert('<?php echo JText::_('WRD_PROMOTION_ALERT_PLEASE_CHOOSE_AN_ITEM')?>');
			}
		});
	});
</script>
<div class="div_wrd_product_of_category">
	<br clear="right" />
	<table  width="100%">
		<tr>
			<td class="a-left">	
				<div class="f-left" style="padding-bottom:3px;font-size:15px;"><label for="title"><?php echo $this->p_title ; ?></label></div>
				<div class="f-right" style="padding-bottom:3px;font-size:15px;"><label for="title">
					You have earned <span class='link_color'><?php echo $this->p_o_u ; ?> points</span></label>
				</div>
				<hr style="clear:both ;" />
			</td>
		</tr>
		<tr>
			<td></td>
		</tr>
	</table>
	<div id="ajax-loading-ck">
		<div class="selectProvider">
			<a name="listTop"></a>
			<div class="tools top">
				<table width="100%" cellspacing="0" cellpadding="0">
					<tbody>
						<tr>
							<td width="160"><span><h2><?php echo $this->cat_name ; ?></h2></span></td>
							<td width="145"></td>
						</tr>
					</tbody>
				</table>
			</div>
				<div class="SPList">
					<div id="selectad" class="selectad s1 "></div>
					<?php $i=0;
							foreach($list_products as $product):
								$images = $list_image[$product->id];
					?>
					<div class="sp">
						<ul style="list-style-type: none;">
							<li class="sp-image">
								<div class="image">
									<a href="index.php?option=com_wrd&view=redemption&layout=product&cid=<?php echo $product->cat_id ; ?>&pid=<?php echo $product->id ; ?>&Itemid=126">
										<img src="wrd/thumb/<?php echo $images->file_name ;?>" class="productImage" alt="Product Details">
									</a>
								</div>
							</li>
							<li class="sp-id">
								<p class="sp-name">
									<a class="title" href="index.php?option=com_wrd&view=redemption&layout=product&cid=<?php echo $product->cat_id ; ?>&pid=<?php echo $product->id ; ?>&Itemid=126">
										<?php echo $product->products_name ; ?>
									</a>
								</p>
								<span class="sp-expertise">
									<?php 
                                                if(!empty($product->point)){
                                                    echo 'Normal: '.$product->point.' Points' ; 
                                                }
                                    ?>        
								</span>
								<span class="sp-expertise">
									<?php 
                                                if(!empty($product->pointx)){ 
                                                    echo "Fast Track: ".$product->pointx ;  ?> Points + <?php echo $product->cash ; ?> VND<?php 
                                                } 
                                     ?>
								</span>
							</li>
							<?php if(!empty($product->quantity)): ?> 
			                     <li class="sp-id">
									<span style="padding-left: 1px;padding-top: 20px;">
										<input type="text" value="0" style="width: 15px" rel1="<?php echo $product->id ; ?>" rel2="" id="qty<?php echo $i;?>">
									</span>
									<button class="button btn-green point" type="button" rel1="<?php echo $product->point ; ?>" rel2="<?php echo $product->cash ; ?>" qty="#qty<?php echo $i;?>" destination="#shortDesc<?php echo $i;?>">
										<span><span>Redeem by point</span></span>
									</button>
									<button class="button btn-green money" type="button" rel1="<?php echo $product->pointx ; ?>" rel2="<?php echo $product->cash ; ?>" qty="#qty<?php echo $i;?>" destination="#shortDesc<?php echo $i;?>">
										<span><span>point and cash</span></span>
									</button>
									<br/>
								</li>
			                <?php else: ?>
			                    <li class="sp-id">
									<span class="out_of_stock"><i>Out of stock</i></span>
								</li>
			                <?php endif; ?>
						</ul>
						<div id="shortDesc<?php echo $i;?>" class="displayPointAndCash" style="position: absolute;margin-left: 500px;"></div>
					</div>
					<?php $i++;
							endforeach;
					?>
				</div>
			<div class="sp-join">
				<button id="submitCart">Add Selections to cart</button>
				<a href="###" style="float: right; margin-top: 2px">&nbsp;</a>
				<button id="clearCart">Clear Form</button>
				</a>
			</div>
		</div>
	</div>
</div>